//后端
package main

import (
	"fmt"
	"net/http"

	"github.com/gin-gonic/gin"
)

type Test struct {
	Name     string  `json:"name" binding:"required"`
	Intval   int32   `json:"intval" binding:"required"`
	Floatval float32 `json:"floatval"`
	BoolVal  bool    `json:"boolval"`
}

func main() {
	var r = gin.Default()

	r.POST("ask", func(c *gin.Context) {
		json := struct {
			Array []Test
		}{}
		err := c.BindJSON(&json)
		if err != nil {
			fmt.Println(err)
		}
		fmt.Println(json)
		//fmt.Println(json.Intval + 10)
		c.JSON(http.StatusOK, gin.H{
			"result": "success",
		})
	})
	r.LoadHTMLGlob("test.html")
	r.GET("index", func(c *gin.Context) {
		c.HTML(http.StatusOK, "test.html", gin.H{})
	})

	r.Run(":8090")

}

//前端
<!DOCTYPE html>
<html lang="en">

<head>
<meta charset="UTF-8">
<title>Title</title>
<link rel="stylesheet" href="https://cdn.acwing.com/static/jquery-ui-dist/jquery-ui.min.css">
<script src="https://cdn.acwing.com/static/jquery/js/jquery-3.3.1.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/axios/dist/axios.min.js"></script>
</head>

<body>
<button class="test">
test
</button>
<script>
$(document).ready(function () {
$(".test").click(


function () {
console.log(
JSON.stringify({
name: "test",
intval: 123,
floatval: 1.11,
})
)

$.ajax({
url: "http://localhost:8090/ask",
type: 'post',
dataType: 'json',
contentType: "application/json",
data: JSON.stringify({
array: [{
name: "test",
intval: 123,
floatval: 1.11,
boolval: true,
},
{
name: "test",
intval: 123,
floatval: 1.11,
boolval: true,
},
{
name: "test",
intval: 123,
floatval: 1.11,
boolval: true,
}

]
}),
success: function (resp) {

}
});

}
)
})
</script>
</body>

</html>
